package com.atguigu.yygh.user.controller.user;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.JwtHelper;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:hx
 * @DateTime:2022/11/14 21:29
 * @Description:
 */
@RestController
@RequestMapping("/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    /**
     * @Description 添加病人
     * @param patient:
     * @param token:
     * @return: com.atguigu.yygh.common.result.R
     */
    @PostMapping("/auth/save")
    public R save(@RequestBody Patient patient, @RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    /**
     * @Description 删除病人
     * @param id:
     * @return: com.atguigu.yygh.common.result.R
     */
    @DeleteMapping("/auth/remove/{id}")
    public R remove(@PathVariable Long id){
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        patientService.remove(queryWrapper);
        return R.ok();
    }

    /**
     * @Description 查询所有病人
     * @return: com.atguigu.yygh.common.result.R
     */
    @GetMapping("/auth/findAll")
    public R findAll(@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        List<Patient> list = patientService.getPatientList(userId);
        return R.ok().data("list",list);
    }

    /**
     * @Description 根据id查询病人
     * @param id:
     * @return: com.atguigu.yygh.common.result.R
     */
    @GetMapping("/auth/get/{id}")
    public R getById(@PathVariable Long id){
        Patient patient = patientService.get(id);
        return R.ok().data("patient",patient);
    }

    /**
     * @Description 修改病人信息
     * @param patient:
     * @return: com.atguigu.yygh.common.result.R
     */
    @PutMapping("/auth/update")
    public R update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }


    /**
     * @Description 用于订单获取病人信息
     * @param patientId:
     * @return: com.atguigu.yygh.model.user.Patient
     */
    @GetMapping("/order/get/{patientId}")
    public Patient getByPatientId(@PathVariable Long patientId){
        return patientService.getById(patientId);
    }
}
